package com.ufcg.lvc.client.prototipacao;

import java.util.ArrayList;
//import java.util.regex.Matcher;
//import java.util.regex.Pattern;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.regexp.shared.MatchResult;
import com.google.gwt.regexp.shared.RegExp;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.ufcg.lvc.client.GreetingServiceCRUD;
import com.ufcg.lvc.client.GreetingServiceCRUDAsync;
import com.ufcg.lvc.client.UserData;

public class ConfigurarConta {

	AbsolutePanel panelQuestoes;
	TextBox nomeTB;
	
	TextBox emailTB;
	
	TextBox usuarioTB;
	UserData UD;
	
	public ConfigurarConta(UserData uD) {
		UD  =uD;
		
		getServicoCRUD().getDadosUsuario(UD.getId_usuario(), new AsyncCallback<ArrayList<String>>() {
			
			@Override
			public void onSuccess(ArrayList<String> result) {
				nomeTB.setText(result.get(0).toString());
				emailTB.setText(result.get(1).toString());
				usuarioTB.setText(result.get(2).toString());
			}
			
			@Override
			public void onFailure(Throwable caught) {
				System.out.println("Erro ao carregar nome, email e login do usuário.");
			}
		});
		
		panelQuestoes = new AbsolutePanel();
		panelQuestoes.setSize("900px", "707px");

		Label titulo = new Label("Configurar conta");
		titulo.setStyleName("gwt-TituloMedio");
		panelQuestoes.add(titulo, 0, 10);
		
		nomeTB = new TextBox();
		panelQuestoes.add(nomeTB, 286, 84);
		nomeTB.setSize("358px", "17px");
		
		emailTB = new TextBox();
		panelQuestoes.add(emailTB, 286, 124);
		emailTB.setSize("358px", "17px");
		
		usuarioTB = new TextBox();
		panelQuestoes.add(usuarioTB, 286, 164);
		usuarioTB.setSize("213px", "17px");
		
		final PasswordTextBox senhaAntigaTB = new PasswordTextBox();
		panelQuestoes.add(senhaAntigaTB, 286, 204);
		senhaAntigaTB.setSize("213px", "17px");
		
		final PasswordTextBox senhaTB = new PasswordTextBox();
		panelQuestoes.add(senhaTB, 286, 244);
		senhaTB.setSize("213px", "17px");
		
		final PasswordTextBox senha2TB = new PasswordTextBox();
		panelQuestoes.add(senha2TB, 286, 284);
		senha2TB.setSize("213px", "17px");
		
		Label label = new Label("Nome completo:");
		panelQuestoes.add(label, 82, 84);
		label.setSize("118px", "17px");
		
		Label label_1 = new Label("E-mail:");
		panelQuestoes.add(label_1, 82, 124);
		label_1.setSize("50px", "17px");
		
		Label label_2 = new Label("Login:");
		panelQuestoes.add(label_2, 82, 164);
		label_2.setSize("59px", "17px");
		
		Label lblSenhaAntiga = new Label("Senha antiga:");
		panelQuestoes.add(lblSenhaAntiga, 82, 204);
		lblSenhaAntiga.setSize("103px", "17px");
		
		Label lblNovaSenha = new Label("Nova senha:");
		panelQuestoes.add(lblNovaSenha, 82, 244);
		lblNovaSenha.setSize("118px", "17px");
		
		Label lblConfirmarNovaSenha = new Label("Confirmar nova senha:");
		panelQuestoes.add(lblConfirmarNovaSenha, 82, 284);
		lblConfirmarNovaSenha.setSize("174px", "17px");
		
		Button btnSalvar = new Button("Salvar alterações");
//		panelQuestoes.add(button, 286, 654);
		panelQuestoes.add(btnSalvar, 286, 371);
		btnSalvar.setSize("156px", "29px");
		
		Button btnCancelar = new Button("Cancelar");
//		panelQuestoes.add(button_1, 448, 654);
		panelQuestoes.add(btnCancelar, 448, 371);
		btnCancelar.setSize("156px", "29px");
		
		btnCancelar.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				panelQuestoes.clear();
			}
		});
		
		btnSalvar.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				if (nomeTB.getText().trim().equals("") || emailTB.getText().trim().equals("") || usuarioTB.getText().trim().equals("") || senhaTB.getText().trim().equals("") || senha2TB.getText().trim().equals("")){
					com.google.gwt.user.client.Window.alert("Todos os campos devem ser preenchidos.");		
				}
				else if(nomeTB.getText().length() < 4){
					com.google.gwt.user.client.Window.alert("O nome deve ter no mínimo 4 caracteres.");
				}
				else if(!isEmailValido(emailTB.getText())){
					com.google.gwt.user.client.Window.alert("Este email não é válido.");
				}
				else if(usuarioTB.getText().length() < 4){
					com.google.gwt.user.client.Window.alert("O login deve ter no mínimo 4 caracteres.");
				}
				else if(senhaTB.getText().length() < 4){
					com.google.gwt.user.client.Window.alert("A senha deve ter no mínimo 4 caracteres.");
				}
				else if(!senhaTB.getText().equals(senha2TB.getText())){
					com.google.gwt.user.client.Window.alert("Senhas não correspondem.");
				}
				else {
					getServicoCRUD().updateUsuario(nomeTB.getText(), usuarioTB.getText(), emailTB.getText(), senhaTB.getText(), senhaAntigaTB.getText(), UD.getId_usuario(), new AsyncCallback<Boolean>() {
						@Override
						public void onSuccess(Boolean result) {
							if (result == true) {
								com.google.gwt.user.client.Window.alert("Dados alterados com sucesso.");
								
							}else{
								com.google.gwt.user.client.Window.alert("Senha incorreta.");	
							}
						}
						@Override
						public void onFailure(Throwable caught) {
							com.google.gwt.user.client.Window.alert("Ocorreu um erro ao alterar os dados. Tente novamente mais tarde.");
						}
					});
				}
			}
		});
//		TextBox textBox_5 = new TextBox();
//		panelQuestoes.add(textBox_5, 286, 401);
//		textBox_5.setSize("213px", "17px");
//		
//		ListBox listBox = new ListBox();
//		panelQuestoes.add(listBox, 286, 534);
//		listBox.setSize("223px", "27px");
//		
//		TextBox textBox_6 = new TextBox();
//		panelQuestoes.add(textBox_6, 286, 487);
//		textBox_6.setSize("213px", "17px");
//		
//		TextBox textBox_7 = new TextBox();
//		panelQuestoes.add(textBox_7, 286, 444);
//		textBox_7.setSize("213px", "17px");
//		
//		ListBox listBox_1 = new ListBox();
//		panelQuestoes.add(listBox_1, 286, 580);
//		listBox_1.setSize("128px", "27px");
//		
//		ListBox listBox_2 = new ListBox();
//		panelQuestoes.add(listBox_2, 422, 580);
//		listBox_2.setSize("87px", "27px");

		
//		Label label_5 = new Label("Número do catão:");
//		panelQuestoes.add(label_5, 82, 401);
//		label_5.setSize("128px", "17px");
//		
//		Label label_6 = new Label("Nome impresso no cartão:");
//		panelQuestoes.add(label_6, 82, 444);
//		label_6.setSize("188px", "17px");
//		
//		Label label_7 = new Label("Bandeira:");
//		panelQuestoes.add(label_7, 82, 534);
//		label_7.setSize("70px", "17px");
//		
//		Label label_8 = new Label("Código de segurança:");
//		panelQuestoes.add(label_8, 82, 487);
//		label_8.setSize("156px", "17px");
//		
//		Label label_9 = new Label("Vencimento");
//		panelQuestoes.add(label_9, 82, 580);
//		label_9.setSize("86px", "17px");
	}

	public boolean isEmailValido(String email) {
		RegExp p = RegExp.compile("^([a-zA-Z0-9_.\\-+])+@(([a-zA-Z0-9\\-])+\\.)+[a-zA-Z0-9]{2,4}$");						
	    //Pattern p = Pattern.compile("^([a-zA-Z0-9_.\\-+])+@(([a-zA-Z0-9\\-])+\\.)+[a-zA-Z0-9]{2,4}$"); 
		MatchResult m = p.exec(email);
//	    Matcher m = p.matcher(email); 
	    if (m!=null){
	      return true;}
	    else{
	      return false;}
	}
	
	public AbsolutePanel getPanel() {
		return panelQuestoes;
	}
	
	public static GreetingServiceCRUDAsync getServicoCRUD() {
		return GWT.create(GreetingServiceCRUD.class);
	}

}

